<!DOCTYPE HTML>
<html class="x-admin-sm">
<head>
    <th:block th:insert="head/head :: common_head"></th:block>
    <script type="text/javascript" th:src="@{/jsfiles/xm-select.js?t=20813}" charset="utf-8"></script>
</head>
<body onload="init('qxdm')">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card" style="margin: 10px;">
                    <div class="layui-tab layui-tab-brief">
                        <div class="layui-tab-content" >
                            <div class="layui-tab-item layui-show">
                                <form class="layui-form layui-form-pane" autocomplete="off">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>所属菜单：
                                        </label>
                                        <div class="layui-input-block">
                                            <div id="menu" style="width: 413.8px"></div>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>权限名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="qxmc" name="qxmc" class="layui-input" onkeydown="on_key_tab_s('qxdm',event);">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>权限代码：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="qxdm" name="qxdm" class="layui-input" onkeydown="on_key_button('bc',event)">
                                        </div>
                                    </div>
                                </form>
                                <button class="layui-btn" lay-submit="" lay-filter="*" id="bc">
                                    新增存盘
                                </button>
                                <button type="button" class="layui-btn layui-btn-primary" onclick="window.location.href='query?syhc=1'">返回上页</button>
                                <div style="height:100px;"></div>
                            </div>

                        </div>
                    </div>
                </div>
        </div>
    </div>
</div>
</div>
<th:block th:insert="head/head :: common_jsfiles"></th:block>
<script>
    /**
     * lx 0 表示新增  1 表示修改 id 为主键
     * 所有增改页面，载入时，都是通过doFirst回调renderForm
     *
     */
    var lx="[[${lx}]]";
    var id="[[${id}]]";
    var url = "[[${serverUrl}]]/sys/per/";
    var ms="t_czy_add";
    $(document).ready(function() {
        if(lx=="1"){
            $("#bc").html("修改存盘");
        }else{
            $("#bc").html("新增存盘")
        }
    });
    function on_result(result)
    {
        if (result.code!=500)
        {
           if (result.code==1 || result.code==2){
                //新增成功
                layer.msg(result.msg, { time: 1000 }, function() {
                    window.location.href='query?syhc=1'
                });//失败时只提示信息，不关闭弹出层且不刷新父页
            } else {
               layer.msg(result.msg);
               $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
            }
        }
        else
        {
            layer.msg(result.msg);
            $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
        }
    }
    //表单相关
    layui.use(['form'], function(){
        var form = layui.form;

        var menu;
        //渲染表格参数
        var renderForm = function (formData){
            //父项目
            console.info("formData:"+JSON.stringify(formData));

            if (lx==1){
                console.info("查询得到的数据:"+JSON.stringify(formData));
                $("#qxmc").val(formData.tPermission.name);
                $("#qxdm").val(formData.tPermission.code);
            }

            var dId;
            if (!is_null(formData.tPermission)) dId = formData.tPermission.menuId;
            else dId="";

            //产品分类树
            menu = xmSelect.render({
                el: '#menu',
                autoRow: false,//自动换行
                radio: true,//开启单选
                initValue: [dId],//默认值
                filterable: true,
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: true,
                    strict: false, //不遵循父子结构
                },
                filterable: true,//开启搜索
                height: 'auto',
                data: function(){
                    return formData.menuTree
                }
            });

            form.render(); // 重新渲染

        }

        function doFirst(){
            //发送的请求
            var jsonData = {
                id: id,
                lx: lx,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_form(url+"info",JSON.stringify(sendStr),"数据加载中...",renderForm);
        }

        function onSave(nonce_zhi){
            //获取提交令牌
            if (is_null(nonce_zhi)){
                $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
                layer.msg("没有获取到操作凭证！请重试！",{icon:2,anim:6});
                return false;
            }
            var selectArr = menu.getValue('valueStr');
            var jsonData = {
                qxdm: $("#qxdm").val(),
                qxmc: $("#qxmc").val(),
                menu: selectArr,
                lx: lx,
                id: id,
                nonce_ms:ms,
                nonce_zhi:nonce_zhi,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_token(url+"save",JSON.stringify(sendStr),"数据提交中...");
        }

        //监听提交
        form.on('submit(*)', function(data){
            if ($("#qxmc").val()==""){
                layer.msg("请输入权限名称！",{icon:2,anim:6});
                init('qxmc');
                return false;
            }
            if ($("#qxdm").val()==""){
                layer.msg("请输入权限代码！",{icon:2,anim:6});
                init_select('qxdm');
                return false;
            }
            if (!confirm("是否确定操作？")) return false;

            $('#bc').addClass("layui-btn-disabled").attr("disabled",true);
            psot_get_nonce(ms,"[[${serverUrl}]]/getnonce",onSave)
            return false;
        });

        doFirst();
    });

</script>
</body>
</html>